package org.vectrics.recruiter.reporting;

import java.awt.Color;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.Locale;
import java.util.Set;

import org.apache.log4j.Logger;
import org.vectrics.VectricsException;
import org.vectrics.common.db.Criteria;
import org.vectrics.domain.Domain;
import org.vectrics.hibernate.ThreadProperties;
import org.vectrics.location.Address;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.user.ContactInfo;
import org.vectrics.user.Person;
import org.vectrics.user.PersonSearchCriteria;
import org.vectrics.user.UserPrivilege;

import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;

/**
 * 
 * @author mmoore
 */
public class EmployeeListReport extends RecruitBaseReport {
    private transient static Logger log = Logger.getLogger(EmployeeListReport.class);

    public static int MAX_COUNT = 10000;

    private SimpleDateFormat historyDateFormat = new SimpleDateFormat("dd/MM/yyyy");

    private PdfPTable userTable = new PdfPTable(5);

    private BaseFont FOOTER_FONT = null;

    private Color ACTIVE_COLOR = new Color(255, 248, 210);

    public EmployeeListReport(final Domain domain, final Locale locale) {
        super(domain, locale);
        try {
            FOOTER_FONT = BaseFont.createFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        } catch (Exception de) {
            log.error("Exception creating footer font", de);
        }
    }

    @Override
    protected Rectangle getPageSize() {
        return PageSize.A4.rotate();
    }

    @Override
    protected void fill(final Document document) throws VectricsException {
        log.debug("User List Report - building");
        try {
            document.add(new Paragraph("User List", ReportConstants.titleFont));

            log.debug("fill() - setting table properties");
            userTable.setHorizontalAlignment(Element.ALIGN_LEFT);
            userTable.setWidthPercentage(100);
            int userTableWidths[] = { 80, 53, 120, 80, 80 };
            userTable.setWidths(userTableWidths);

            log.debug("fill() - adding table header row");
            addTableHeaderCell(userTable, "prompt.person.name");
            addTableHeaderCell(userTable, "prompt.user.name");
            addTableHeaderCell(userTable, "prompt.address");
            addTableHeaderCell(userTable, "prompt.user.roles");
            addTableHeaderCell(userTable, "prompt.person.summary");
            userTable.setHeaderRows(1);

            log.debug("fill() - adding user rows");
            PersonSearchCriteria criteria = CoreServiceLocator.createPersonSearchCriteria();
            Set domains = CoreServiceLocator.getPersonService().findDomainsWithUserPrivilege(
                    ThreadProperties.getPerson(), Person.PRIVILEGE_PERSON_VIEW);
            criteria.setDomains(domains);

            criteria.setVoidedType(Criteria.VOID_NO);
            Iterator iter = CoreServiceLocator.getPersonService().findPersons(criteria, 0, MAX_COUNT).iterator();
            while (iter.hasNext()) {
                Person person = (Person) iter.next();
                log.debug("fill() - adding user...");
                addUserRow(person);
            }
            log.debug("fill() - adding user table to document");
            document.add(userTable);

        } catch (Exception e) {
            log.error("Exception creating pdf document - in fill()", e);
            throw new VectricsException("Exception creating pdf document - fill()", e);
        }

    }

    protected void setupHeaderFooter(final Document document) throws VectricsException {
        log.debug("setupHeaderFooter()");
    }

    protected void addTableHeaderCell(final String titleCode, final Locale locale) {
        String title = CoreServiceLocator.getPhraseProvider().getMessage(titleCode, locale);
        log.debug("addTableHeaderCell()");
        PdfPCell hdrCell = new PdfPCell(new Phrase(title, ReportConstants.stdBoldFont));
        hdrCell.setBackgroundColor(ReportConstants.TABLE_HEAD_COLOR);
        userTable.addCell(hdrCell);
    }

    protected void addUserRow(final Person person) {
        Color color = new Color(255, 255, 255);

        PdfPCell cell = new PdfPCell(new Phrase(person.getName(), ReportConstants.stdFont));
        cell.setBackgroundColor(color);
        userTable.addCell(cell);

        cell = new PdfPCell(new Phrase(person.getUser().getUserName(), ReportConstants.stdFont));
        cell.setBackgroundColor(color);
        userTable.addCell(cell);

        String addressStr = "";
        ContactInfo contactInfo = person.getContactInfo();
        if (contactInfo != null) {
            Address address = contactInfo.getHomeAddress();
            if (address != null) {
                addressStr = address.getAddress1() + " - " + address.getCity() + ", " + address.getState();
            }
        }
        cell = new PdfPCell(new Phrase(addressStr, ReportConstants.stdFont));
        cell.setBackgroundColor(color);
        userTable.addCell(cell);

        if (person.getUser() != null) {
            String roles = "";
            Iterator roleIter = person.getPrivileges().iterator();
            while (roleIter.hasNext()) {
                UserPrivilege userPrivilege = (UserPrivilege) roleIter.next();
                String role = userPrivilege.getPrivilegeType().getName();
                if (roles.length() > 0) {
                    roles += ", ";
                }

                roles += role;
            }
            cell = new PdfPCell(new Phrase(roles, ReportConstants.stdFont));
        }
        cell.setBackgroundColor(color);
        userTable.addCell(cell);

        String notes = "";
        cell = new PdfPCell(new Phrase(notes, ReportConstants.stdFont));
        cell.setBackgroundColor(color);
        userTable.addCell(cell);
    }

}
